package com.qyer.commons.mc.reloader;

import com.qyer.commons.mc.converter.Converter;
import com.qyer.commons.persistence.AbstractRedisOperator;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * User: Z J Wu Date: 2015/08/12 Time: 22:15 Package: com.qyer.dsp.cache
 */
public class RedisReloader<T> extends Reloader<T> {

  private static final Logger LOGGER = LoggerFactory.getLogger(RedisReloader.class);

  private Converter<String, T> converter;

  private final AbstractRedisOperator operator;

  public RedisReloader(AbstractRedisOperator operator, String cacheKey,
                       Converter<String, T> converter) {
    super(cacheKey);
    this.operator = operator;
    this.converter = converter;
  }

  @Override public T _reload() throws Exception {
    String cacheKey = getReloadResource();
    String valueString = operator.get(cacheKey);
    if (StringUtils.isBlank(valueString)) {
      LOGGER.warn("Content of {} is null.", cacheKey);
    }
    return converter.convert(valueString);
  }
}
